import { createRouter, createWebHistory } from 'vue-router'
import type { Router } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '@/views/login/LoginView.vue'
import 'nprogress/nprogress.css'

import NProgress from 'nprogress'
const router: Router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),

  routes: [
    {
      path: '/',
      name: 'login',
      component: LoginView
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView,
      redirect: "/home/dashboard",
      children: [{
        path: "dashboard",
        name: "dashboard",
        component: () => import('../views/dashboard/index.vue')
      },

      {

        path: '/:catchAll(.*)',
        name: 'error',
        component: () => import('../views/404.vue')
      },
      ]

    },


    // {
    //   path: '/about',
    //   name: 'about',
    //   component: () => import('../views/AboutView.vue')
    // }
  ]



})


import { useUserStore } from '@/stores/user'



router.beforeEach((to, from, next) => {

  NProgress.start();


  // console.log(to)
  const { isLogin } = useUserStore()
  if (isLogin || to.path === '/') {

    next()

    NProgress.done();
  } else {
    NProgress.done();
    next('/')
  }



})


export default router
